import { Controller, Post, Body } from '@nestjs/common';
import { AuthService } from './auth.service';
import { SendCodeDto } from './dto/send-code.dto';
import { RegisterDto } from './dto/register.dto';
import { LoginWithPasswordDto } from './dto/login-with-password.dto';
import { LoginWithCodeDto } from './dto/login-with-code.dto';

/**
 * 认证控制器
 * 处理用户登录、注册和验证码发送等认证相关API请求
 */
@Controller('auth')
export class AuthController {
  constructor(private readonly authService: AuthService) {}

  /**
   * 发送手机验证码
   * @param sendCodeDto - 包含手机号的请求体
   * @returns 发送状态和消息
   * @example 请求体: {"phone":"13800138000"}
   * @example 响应: {"success":true,"message":"验证码已发送"}
   */
  @Post('sendCode')
  sendCode(@Body() sendCodeDto: SendCodeDto) {
    return this.authService.sendCode(sendCodeDto);
  }

  /**
   * 用户注册
   * @param registerDto - 包含手机号、密码和验证码的请求体
   * @returns 包含token和用户信息的对象
   * @example 请求体: {"phone":"13800138000","code":"123456","password":"password123"}
   * @example 响应: {"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...","user":{"id":1,"username":"用户8000","phone":"13800138000",...}}
   */
  @Post('register')
  async register(@Body() registerDto: RegisterDto) {
    return this.authService.register(registerDto);
  }

  /**
   * 手机号密码登录
   * 验证手机号和密码，成功后返回token
   * @param loginWithPasswordDto - 包含手机号和密码的请求体
   * @returns 包含token和用户信息的对象
   * @example 请求体: {"phone":"13800138000","password":"password123"}
   * @example 响应: {"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...","user":{"id":1,"username":"用户8000","phone":"13800138000",...}}
   */
  @Post('login/password')
  async loginWithPassword(@Body() loginWithPasswordDto: LoginWithPasswordDto) {
    return this.authService.loginWithPassword(loginWithPasswordDto);
  }

  /**
   * 手机号验证码登录
   * 验证手机号和验证码，成功后返回token
   * @param loginWithCodeDto - 包含手机号和验证码的请求体
   * @returns 包含token和用户信息的对象
   * @example 请求体: {"phone":"13800138000","code":"123456"}
   * @example 响应: {"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...","user":{"id":1,"username":"用户8000","phone":"13800138000",...}}
   */
  @Post('login/code')
  async loginWithCode(@Body() loginWithCodeDto: LoginWithCodeDto) {
    return this.authService.loginWithCode(loginWithCodeDto);
  }
}
